/* add user code begin Header */
/**
  **************************************************************************
  * @file     at32f421_int.c
  * @brief    main interrupt service routines.
  **************************************************************************
  *                       Copyright notice & Disclaimer
  *
  * The software Board Support Package (BSP) that is made available to
  * download from Artery official website is the copyrighted work of Artery.
  * Artery authorizes customers to use, copy, and distribute the BSP
  * software and its related documentation for the purpose of design and
  * development in conjunction with Artery microcontrollers. Use of the
  * software is governed by this copyright notice and the following disclaimer.
  *
  * THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
  * GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
  * TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
  * STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
  * INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
  *
  **************************************************************************
  */
/* add user code end Header */

/* includes ------------------------------------------------------------------*/
#include "at32f421_int.h"
#include "wk_system.h"

/* private includes ----------------------------------------------------------*/
/* add user code begin private includes */
#include "at32f421_wk_config.h"
/* add user code end private includes */

/* private typedef -----------------------------------------------------------*/
/* add user code begin private typedef */
extern uint8_t usart1_tx_dma_status;
extern uint8_t usart1_rx_dma_status;
extern uint8_t usart1_rx_buffer;
extern uint16_t usart1_rx_buffer_size;
extern uint8_t g_ucDataReady; // 1: data ready

extern uint8_t Darllen_amserol_ADC_flag;
extern uint8_t DIM_flag ;

extern uint16_t Taith_out_R;
extern uint16_t Taith_out_G;
extern uint16_t Taith_out_B;

uint8_t demy_us = 0;

uint16_t amser_out_R = 0;  
uint16_t amser_out_G= 0;
uint16_t amser_out_B = 0;

/* add user code end private typedef */

/* private define ------------------------------------------------------------*/
/* add user code begin private define */

/* add user code end private define */

/* private macro -------------------------------------------------------------*/
/* add user code begin private macro */

/* add user code end private macro */

/* private variables ---------------------------------------------------------*/
/* add user code begin private variables */

/* add user code end private variables */

/* private function prototypes --------------------------------------------*/
/* add user code begin function prototypes */

/* add user code end function prototypes */

/* private user code ---------------------------------------------------------*/
/* add user code begin 0 */

/* add user code end 0 */

/* external variables ---------------------------------------------------------*/
/* add user code begin external variables */

/* add user code end external variables */

/**
  * @brief  this function handles nmi exception.
  * @param  none
  * @retval none
  */
void NMI_Handler(void)
{
  /* add user code begin NonMaskableInt_IRQ 0 */

  /* add user code end NonMaskableInt_IRQ 0 */

  /* add user code begin NonMaskableInt_IRQ 1 */

  /* add user code end NonMaskableInt_IRQ 1 */
}

/**
  * @brief  this function handles hard fault exception.
  * @param  none
  * @retval none
  */
void HardFault_Handler(void)
{
  /* add user code begin HardFault_IRQ 0 */

  /* add user code end HardFault_IRQ 0 */
  /* go to infinite loop when hard fault exception occurs */
  while (1)
  {
    /* add user code begin W1_HardFault_IRQ 0 */

    /* add user code end W1_HardFault_IRQ 0 */
  }
}

/**
  * @brief  this function handles memory manage exception.
  * @param  none
  * @retval none
  */
void MemManage_Handler(void)
{
  /* add user code begin MemoryManagement_IRQ 0 */

  /* add user code end MemoryManagement_IRQ 0 */
  /* go to infinite loop when memory manage exception occurs */
  while (1)
  {
    /* add user code begin W1_MemoryManagement_IRQ 0 */

    /* add user code end W1_MemoryManagement_IRQ 0 */
  }
}

/**
  * @brief  this function handles bus fault exception.
  * @param  none
  * @retval none
  */
void BusFault_Handler(void)
{
  /* add user code begin BusFault_IRQ 0 */

  /* add user code end BusFault_IRQ 0 */
  /* go to infinite loop when bus fault exception occurs */
  while (1)
  {
    /* add user code begin W1_BusFault_IRQ 0 */

    /* add user code end W1_BusFault_IRQ 0 */
  }
}

/**
  * @brief  this function handles usage fault exception.
  * @param  none
  * @retval none
  */
void UsageFault_Handler(void)
{
  /* add user code begin UsageFault_IRQ 0 */

  /* add user code end UsageFault_IRQ 0 */
  /* go to infinite loop when usage fault exception occurs */
  while (1)
  {
    /* add user code begin W1_UsageFault_IRQ 0 */

    /* add user code end W1_UsageFault_IRQ 0 */
  }
}

/**
  * @brief  this function handles svcall exception.
  * @param  none
  * @retval none
  */
void SVC_Handler(void)
{
  /* add user code begin SVCall_IRQ 0 */

  /* add user code end SVCall_IRQ 0 */
  /* add user code begin SVCall_IRQ 1 */

  /* add user code end SVCall_IRQ 1 */
}

/**
  * @brief  this function handles debug monitor exception.
  * @param  none
  * @retval none
  */
void DebugMon_Handler(void)
{
  /* add user code begin DebugMonitor_IRQ 0 */

  /* add user code end DebugMonitor_IRQ 0 */
  /* add user code begin DebugMonitor_IRQ 1 */

  /* add user code end DebugMonitor_IRQ 1 */
}

/**
  * @brief  this function handles pendsv_handler exception.
  * @param  none
  * @retval none
  */
void PendSV_Handler(void)
{
  /* add user code begin PendSV_IRQ 0 */

  /* add user code end PendSV_IRQ 0 */
  /* add user code begin PendSV_IRQ 1 */

  /* add user code end PendSV_IRQ 1 */
}

/**
  * @brief  this function handles systick handler.
  * @param  none
  * @retval none
  */
void SysTick_Handler(void)
{
  /* add user code begin SysTick_IRQ 0 */
  demy_us++;
  /* add user code end SysTick_IRQ 0 */

  wk_timebase_handler();

  /* add user code begin SysTick_IRQ 1 */

  /* add user code end SysTick_IRQ 1 */
}

/**
  * @brief  this function handles EXINT Line 1 & 0 handler.
  * @param  none
  * @retval none
  */
void EXINT1_0_IRQHandler(void)
{
  /* add user code begin EXINT1_0_IRQ 0 */
  if(exint_flag_get(EXINT_LINE_1))
  {
    exint_flag_clear(EXINT_LINE_1);
    printf("EXINT1_0_IRQHandler\r\n");
  }
  /* add user code end EXINT1_0_IRQ 0 */
  /* add user code begin EXINT1_0_IRQ 1 */

  /* add user code end EXINT1_0_IRQ 1 */
}

/**
  * @brief  this function handles DMA1 Channel 3 & 2 handler.
  * @param  none
  * @retval none
  */
void DMA1_Channel3_2_IRQHandler(void)
{
  /* add user code begin DMA1_Channel3_2_IRQ 0 */
  if(dma_interrupt_flag_get(DMA1_FDT2_FLAG))
  {
    usart1_tx_dma_status = 1;
    dma_flag_clear(DMA1_FDT2_FLAG);
    dma_channel_enable(DMA1_CHANNEL2, FALSE);
  }
  else if(dma_interrupt_flag_get(DMA1_FDT3_FLAG))
  {
    usart1_rx_dma_status = 1;
    dma_flag_clear(DMA1_FDT3_FLAG);
    dma_channel_enable(DMA1_CHANNEL3, FALSE);
    dma_data_number_set(DMA1_CHANNEL3,DMA1_CHANNEL3_BUFFER_SIZE);  
    dma_channel_enable(DMA1_CHANNEL3, TRUE);       
    usart1_rx_buffer_size = DMA1_CHANNEL3_BUFFER_SIZE;       
  }
  /* add user code end DMA1_Channel3_2_IRQ 0 */
  /* add user code begin DMA1_Channel3_2_IRQ 1 */

  /* add user code end DMA1_Channel3_2_IRQ 1 */
}

/**
  * @brief  this function handles DMA1 Channel 5 & 4 handler.
  * @param  none
  * @retval none
  */
void DMA1_Channel5_4_IRQHandler(void)
{
  /* add user code begin DMA1_Channel5_4_IRQ 0 */
  if(dma_interrupt_flag_get(DMA1_FDT5_FLAG))
  {
    g_ucDataReady = 1;  // 触发数据处理
    dma_flag_clear(DMA1_FDT5_FLAG);
    // dma_channel_enable(DMA1_CHANNEL4, FALSE);
    // dma_data_number_set(DMA1_CHANNEL4,DMA1_CHANNEL4_BUFFER_SIZE);
    // dma_channel_enable(DMA1_CHANNEL4, TRUE);
  }
  /* add user code end DMA1_Channel5_4_IRQ 0 */
  /* add user code begin DMA1_Channel5_4_IRQ 1 */

  /* add user code end DMA1_Channel5_4_IRQ 1 */
}

/**
  * @brief  this function handles TMR3 handler.
  * @param  none
  * @retval none
  */
void TMR3_GLOBAL_IRQHandler(void)
{
  /* add user code begin TMR3_GLOBAL_IRQ 0 */

  /* add user code end TMR3_GLOBAL_IRQ 0 */


  /* add user code begin TMR3_GLOBAL_IRQ 1 */

  /* add user code end TMR3_GLOBAL_IRQ 1 */
}

/**
  * @brief  this function handles TMR14 handler.
  * @param  none
  * @retval none
  */
void TMR14_GLOBAL_IRQHandler(void)
{
  /* add user code begin TMR14_GLOBAL_IRQ 0 */
  if (tmr_flag_get(TMR14, TMR_OVF_FLAG) != RESET)
  {
    tmr_flag_clear(TMR14, TMR_OVF_FLAG);
    if (Taith_out_R != amser_out_R)
    {
      if ( Taith_out_R > amser_out_R)
      {
        amser_out_R++;
      }
      else if ( Taith_out_R < amser_out_R)
      {
        amser_out_R--;
      }
    }
    if (Taith_out_G != amser_out_G)
    {
      if ( Taith_out_G > amser_out_G)
      {
        amser_out_G++;
      }
      else if ( Taith_out_G < amser_out_G)
      {
        amser_out_G--;
      }
    }
    if (Taith_out_B != amser_out_B)
    {
      if ( Taith_out_B > amser_out_B)
      {
        amser_out_B++;
      }
      else if ( Taith_out_B < amser_out_B)
      {
        amser_out_B--;
      }
    }
    tmr_channel_value_set(TMR3, TMR_SELECT_CHANNEL_4, amser_out_R); 
    tmr_channel_value_set(TMR3, TMR_SELECT_CHANNEL_1, amser_out_G);
    tmr_channel_value_set(TMR3, TMR_SELECT_CHANNEL_2, amser_out_B);
  }
  /* add user code end TMR14_GLOBAL_IRQ 0 */


  /* add user code begin TMR14_GLOBAL_IRQ 1 */

  /* add user code end TMR14_GLOBAL_IRQ 1 */
}

/**
  * @brief  this function handles TMR16 handler.
  * @param  none
  * @retval none
  */
void TMR16_GLOBAL_IRQHandler(void)
{
  /* add user code begin TMR16_GLOBAL_IRQ 0 */
  if (tmr_flag_get(TMR16, TMR_OVF_FLAG) != RESET)
  {
    tmr_flag_clear(TMR16, TMR_OVF_FLAG);
    DIM_flag = 1;
  }
  /* add user code end TMR16_GLOBAL_IRQ 0 */


  /* add user code begin TMR16_GLOBAL_IRQ 1 */

  /* add user code end TMR16_GLOBAL_IRQ 1 */
}

/**
  * @brief  this function handles TMR17 handler.
  * @param  none
  * @retval none
  */
void TMR17_GLOBAL_IRQHandler(void)
{
  /* add user code begin TMR17_GLOBAL_IRQ 0 */
  if (tmr_flag_get(TMR17, TMR_OVF_FLAG) != RESET)
  {
    tmr_flag_clear(TMR17, TMR_OVF_FLAG);
    Darllen_amserol_ADC_flag = 1;
  }
  /* add user code end TMR17_GLOBAL_IRQ 0 */


  /* add user code begin TMR17_GLOBAL_IRQ 1 */

  /* add user code end TMR17_GLOBAL_IRQ 1 */
}

/**
  * @brief  this function handles USART1 handler.
  * @param  none
  * @retval none
  */
void USART1_IRQHandler(void)
{
  /* add user code begin USART1_IRQ 0 */
  if(usart_flag_get(USART1,USART_IDLEF_FLAG) == SET)
  {
    usart_flag_clear(USART1, USART_IDLEF_FLAG);
    usart_data_receive(USART1); 
    usart1_rx_buffer_size = DMA1_CHANNEL3_BUFFER_SIZE - dma_data_number_get(DMA1_CHANNEL3);
    
    dma_channel_enable(DMA1_CHANNEL3, FALSE); 
    dma_data_number_set(DMA1_CHANNEL3,DMA1_CHANNEL3_BUFFER_SIZE);                 
    dma_channel_enable(DMA1_CHANNEL3, TRUE);    
    usart1_rx_dma_status = 1; 
  }
  /* add user code end USART1_IRQ 0 */
  /* add user code begin USART1_IRQ 1 */

  /* add user code end USART1_IRQ 1 */
}

/* add user code begin 1 */

/* add user code end 1 */
